/**
 * Created by wanglei@enersun.com.cn on 2016/7/8.
 */

var pageNo=1;
var limit = 12;

function doLoadNewsInfo() {
	$.post("/csg_pic/path/newsReview/listreviewjson", getParamData(),
			function(result) {
				if (result.state == 'Success') {
					clearHtml();
					createListTemplate(result.content);
				} else {
					alert("操作失败,原因："+result.content);
				}
			}
		);
}

function pagnation(totalCounts,totalPages,currentPageNo){
	var pagnation = $("#pagnation");
	var pagnationHtml = "";
	var pagePrevHtml = "";
	var pageNextHtml = "";
	var pageNumberHtml = "";
	pagnationHtml += "<span class=\"pageDown-sum\"><em>共<i>"+totalCounts+"</i>条记录</em><em>当前第 <i>"+currentPageNo+"</i>页 </em><i>/</i>共<i> "+totalPages+"</i>页</span>";
	if(totalPages > 0)
	{
		if(currentPageNo == 1)
		{
			pagePrevHtml = "<a class=\"page-prev\">上一页</a>";
			//总页码为 1，禁止下一页
			if(totalPages==1)
			{
				pageNextHtml = "<a class=\"page-next\">下一页</a>";
			}else{
				pageNextHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo+1)+")\" class=\"page-next\">下一页</a>";	
			}
		}else if(currentPageNo < totalPages){
			pagePrevHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo-1)+")\" class=\"page-prev\">上一页</a>";
			pageNextHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo+1)+")\" class=\"page-next\">下一页</a>";
		}else if(currentPageNo == totalPages){
			pagePrevHtml = "<a href=\"javascript:goPage("+parseInt(currentPageNo-1)+")\" class=\"page-prev\">上一页</a>";
			pageNextHtml = "<a class=\"page-next\">下一页</a>";
		}
		//总页码 <7
		if(totalPages < 7){
			for(var i = 0 ; i < totalPages ; i++){
				var currentClass = "";
				if((i+1) == currentPageNo){
					currentClass = "class = \"current\"";
				}
				pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
			}
		}else{
			//判断是否后几页
			if(totalPages - currentPageNo - 4 > 0){
				var start = 0,end = 0;
				if(currentPageNo  < 3){
					start = 0;end = 6;
				}else{
					start = currentPageNo - 3 ; end = currentPageNo + 3;
				}
				for(var i = start ; i < end ; i++){
					var currentClass = "";
					if((i+1) == currentPageNo){
						currentClass = "class = \"current\"";
					}
					pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
				}
				pageNumberHtml += "<span class=\"page-items\">...</span>";
				pageNumberHtml += "<a href=\"javascript:goPage("+totalPages+")\" >"+totalPages+"</a>";
			}else{
				pageNumberHtml += "<a href=\"javascript:goPage(1)\" >1</a>";
				pageNumberHtml += "<span class=\"page-items\">...</span>";
				for(var i =totalPages - 6 ; i < totalPages ; i++){
					var currentClass = "";
					if((i+1) == currentPageNo){
						currentClass = "class = \"current\"";
					}
					pageNumberHtml += "<a href=\"javascript:goPage("+parseInt(i+1)+")\" "+currentClass+">"+(i+1)+"</a>";
				}
			}
		}
	}
	pagnation.html(pagnationHtml + pagePrevHtml + pageNumberHtml + pageNextHtml);
}

function clearHtml(){
	 $("#reviewList li").each(function(index){
		 var id=$(this).attr("id");
		 if(id!='lastNewsInfo'){
			 $(this).remove();
		 }
     });      
}
//获取查询参数
function getParamData(){
	var title=$("#searchValue_title").val();
	if(title==undefined)
	{
		title='';
	}
	return {"state":reviewState,"pageNo":pageNo,"limit":limit,"title":title};
}
/**
* 根据模板创建新闻信息
*/
function createListTemplate(pageInfo){
	if(pageInfo.items.length>0){
		for(var index=0;index<pageInfo.items.length;index++){
			var newsReviewInfo=pageInfo.items[index];
			var currNews=$('#newsInfoTemplate').html();
			currNews=currNews.replaceAll("%news.id%",newsReviewInfo.busId);
			currNews=currNews.replaceAll("%news.coverPath%","/csg_pic"+newsReviewInfo.coverPath);
			currNews=currNews.replaceAll("%news.createTime%",newsReviewInfo.createTime);
			currNews=currNews.replaceAll("%news.title%",newsReviewInfo.title);
			currNews=currNews.replaceAll("%news.revid%",newsReviewInfo.id);
			if(newsReviewInfo.comments!=undefined){
				currNews=currNews.replaceAll("%news.comments%","审核意见："+newsReviewInfo.comments);
			}else{
				currNews=currNews.replaceAll("%news.comments%","无审核意见");
			}
			if(newsReviewInfo.nabstract!=undefined){
				currNews=currNews.replaceAll("%news.nabstract%",cutString(newsReviewInfo.nabstract));
			}else{
				currNews=currNews.replaceAll("%news.nabstract%","");
			}
			if(newsReviewInfo.type=='pic'){
				currNews=currNews.replaceAll("%news.type%",'<span class="glyphicon glyphicon-picture fn-fl" title="图片新闻"></span>');
				//无图片数量
				currNews=currNews.replaceAll("%news.picNum%",'<em class="mod-itemsNum">'+newsReviewInfo.picNum+'</em>');
			}else{
				currNews=currNews.replaceAll("%news.type%",'<span class="glyphicon glyphicon-text-size fn-fl" title="文字新闻"></span>');
				currNews=currNews.replaceAll("%news.picNum%","");
				
			}
			$('#lastNewsInfo').before(currNews);
			$('#cover_'+newsReviewInfo.busId).error(function(){
				noImage(this);
			});
		}	
		//分页
		pagnation(pageInfo.totalCount,pageInfo.totalPageCount,pageInfo.currentPageNo);
	}else{
		
		//$('#lastNewsInfo').before("<li>没有数据</li>");
	}	
}
function goPage(number){
	pageNo = number;
	doLoadNewsInfo();
}

/**
 * 截取字符
 * @param content
 * @returns
 */
function cutString(content){
	if(content.length>35){
		return content.substr(0, 35)+"......"; 
	}else{
		return content;
	}
}

String.prototype.replaceAll = function(s1,s2) { 
    return this.replace(new RegExp(s1,"gm"),s2); 
}

function noImage(t){
	t.src="/csg_pic/zenithsun/news/mobile/images/Default-bg270x180.png"; 
	t.onerror=null;
}